<?php
class TableArray implements ArrayAccess, Iterator, Countable{
	private $table;
	private $array=array();
	private $currentIndex=0;
	
	function TableArray($name){
		$this->setTable($name);
	}
		
	/*
	 * Bind the results of a query on the associated table to a collection of table  objects
	 */
	function load($condition = null, $orderby = null) {
		if ($condition !== null) $query = "SELECT * FROM `" . $this->table . "` WHERE " . $condition;
		else $query = "SELECT * FROM `" . $this->table . "`";
		if ($orderby !== null) $query .= " ORDER BY `" . $orderby . "`";
		return $this->loadQuery($query);
		
	}
	
	/*
	 * Bind the results of a query to a collection of table objects
	 */
	function loadQuery($query){
		$result=Database::runSelectQuery($query,MYSQL_ASSOC);
		return $this->loadArray($result);
	}
	
/*
	 * Bind a 2d array to a collection of table objects
	 */
	function loadArray($array){
		if($this->bindArray($array)){
			return $this;
		}
		return false;
	}
	
	//Insert an array of table objects into the db
	public static function insert(){
		if(!count($this))
			return false;
		$table=self::allSameTable($array);
		if($table==false||$table==null)
			return false;
		if(!$values=$this->getTableArrayValues()) return  false;
		return Database::insertMultiArray($values,$table);
	}
	
	//Make sure every object is a Table object and that all objects reference the same table
	function allSameTable(){
		if(!count($this))
			return false;
		$table=null;
		$count=0;
		foreach($this as $ob){
			if($count==0)
				$table=$ob->getTable();
			else if($ob->getTable()!=$table)
				return false;
			$count++;
		}
		return $table;
	}
	
	//Get all the fields from an array of table objects
	function toArray(){
		if(!count($this))
			return false;
		$fields=array();
		foreach($this as $ob){
			$fields[]=$ob->getFields();
		}
		return $fields;
	}
	
	//Get a field from each table object in an array of table objects
	public function getFieldValues($field){
		if(!count($this))
			return false;
		$table=self::allSameTable($this->array);
		if($table==false||$table==null)
			return false;
		$temp=array();
		foreach($this as $ob){
			$temp[]=$ob[$field];
		}
		return $temp;
	}
	
	function setTable($name){
		$this->table=$name;
	}
	
	function bindArray($array){
		if(!is_array($array)) return false;
		$count=0;
		foreach($array as $value){
			$this[$count]=new Table($this->table);
			if(!$this[$count]->bind($value)) return false;
			$count++;
		}
		return true;
	}
	
	function store(){
		foreach($this->array as $table){
			if(!$table->store()) return false;
		}
		return true;
	}
	
	function offsetExists($offset) {
		return isset($this->array[$offset]);
	}
	
	/*
	 * Get object property via array access
	 */
	function offsetGet($offset) {
		if(isset($this->array[$offset])){
			return $this->array[$offset];
		}
	    return null;
	}
	
	/*
	 * Set object property via array access
	 */
	function offsetSet($offset, $value) {
		$this->array[$offset]=$value;
	       
	}
	
	function offsetUnset($offset) {
		if(isset($array[$offset]))
			unset($this->array[$offset]);
	}
	
	function count()
   	{
   		return count($this->array);
   	}
       //EndRegion

       //Region Iterator
	function current()
   	{
       return current($this->array);
   	}

	function key()
	{	
		return key($this->array);
	}

	function next()
	{
		return next($this->array);
	}

	function rewind()
	{
		reset($this->array);
	}
	
	function valid()
    {
       	return isset($this->array[$this->key()]);
	}

	function getIterator()
   	{
   		return $this;
   	}	
}
?>